<?php
# Set the error reporting level
#error_reporting ( E_ALL );

# Debug level
define ( '_DEBUG_', TRUE );

# Verify this file is being called internally by Devfly
if ( !defined ( 'DF__SAFE' ) ) die ( 'This file may not be called alone.' );

# Verify PHP version
define ( '_PHP_VERSION_', '5.1.0' );
if ( version_compare ( phpversion ( ), _PHP_VERSION_, '<' ) == TRUE ) die ( 'This framework requires PHP ' . _PHP_VERSION_ . ' or higher.' );

# Start the session
@session_start ( );

# Grab some configuration files
require_once 'config/paths.php';

# Require some files
require_once DIR_DF_CORE . 'controller.php'; // neeeded so application controllers can extend it.

require_once DIR_DF_CORE . 'database.php'; // neeeded so db layers can extend it.

#] Autoload classes. This function is vital to the core.
#] It allows us to instantiate a class without manually
#] requiring the class file.
#] @me: I really need to rework this function. It is not very good =x
#]----------------------------------------------------------
#] $_0: string class name
#]----------------------------------------------------------
function __autoload ( $_0 )  
{
	$filename = strtolower ( $_0 ) . '.php';
	
	$core_path = DIR_DF_CORE . $filename;
	
	$core_lib_path = DIR_DF_LIB . $filename;
	
	$app_lib_path	 = DIR_APP_LIB . $filename;
	
	$controller_path = DIR_APP_CONTROLLERS . $filename;
	
	if ( file_exists ( $core_path ) !== FALSE )
	{
		$file = $core_path;
	}
	else if ( file_exists ( $core_lib_path ) !== FALSE )
	{
		$file = $core_lib_path;
	}
	else if ( file_exists ( $app_lib_path ) !== FALSE )
	{
		$file = $app_lib_path;
	}
	else if ( file_exists ( $controller_path ) !== FALSE )
	{
		$file = $controller_path;
	}
	elseif ( class_exists ( $_0, FALSE ) )
	{
		#>>>>DEBUGGING
		if ( _DEBUG_ )
		{
			echo "__Class already exists: {$_0}<br>";
		}
		
		return FALSE;
	}
	else
	{
		#>>>>DEBUGGING
		if ( _DEBUG_ )
		{
			echo "__Could not load: {$filename}<br>";
		}
		
		return FALSE;
	}
	
	#>>>>DEBUGGING
	if ( _DEBUG_ )
	{
		echo "__Successfully loaded: {$file}<br>";
	}
	
	require $file;
}

# Instantiate some core objects (these are required for devfly to work properly)

//-- Global DF object to pass data throughout the application
$DF = new Devfly; 				

//-- Handles loading of all models, views, controllers, libraries, and DF core classes
$loader = new Loader ( $DF ); 

$DF->set ( 'load', $loader );

//-- Templating. Yay! This templating class is actually uber basic.
$DF [ 'load' ]->Core ( 'template', 'tpl' );

# Now we need to auto load any libraries that are specified in our autoload config.
# This is good for stuff like, db abstraction layers and other libraries that you
# will need to use on every page.
require_once DIR_DF_CONFIG . 'autoload.php';

if ( !empty ( $AUTOLOAD ) )
{
	foreach ( $AUTOLOAD as $row )
	{
		$DF [ 'load' ]->Library( $row [ 0 ], $row [ 1 ] );
	}
}

// @me: add auto loading mess here 

# Load the router to handle the request and route it to the appropriate controller
$DF [ 'load' ]->Core ( 'router', 'router' );

# Set the path to the application's controllers
$DF [ 'router' ]->setPath ( DIR_APP_CONTROLLERS );

# Parse the request, call appropriate controller, and get this party started!
$DF [ 'router' ]->delegate ( );

#>>>>DEBUGGING
if ( _DEBUG_ )
{
	$DF [ 'common' ]->pre ( 
		print_r ( $DF, TRUE ) . 
		print_r ( $_REQUEST, TRUE )
	);
}
?>